import {ipcRenderer} from 'electron';
import {TreeOption} from "naive-ui";
import ResultDTO from "../../model/ResultDTO";
import MySqlConnectInfo from "../../model/mysql/MySqlConnectInfo";
import DispatchEventEnums from "../../enum/DispatchEventEnums";
//DataSourceTree一些操作

//测试链接是否
const testConnect = (connectInfo: MySqlConnectInfo): Promise<ResultDTO<any>> => {
    return new Promise<ResultDTO<any>>((resolve, reject) => {
        if (!connectInfo) {
            reject(ResultDTO.fail("链接信息为空"))
        }
        if (!connectInfo.port || 0 === connectInfo.port) {
            reject(ResultDTO.fail("端口未填写"))
        }
        if (!connectInfo.host) {
            reject(ResultDTO.fail("主机未填写"))
        }
        if (!connectInfo.password) {
            reject(ResultDTO.fail("密码未填写"))
        }
        if (!connectInfo.user) {
            reject(ResultDTO.fail("用户名未填写"))
        }
        ipcRenderer.invoke('test-connect', JSON.stringify(connectInfo))
            .then((result: ResultDTO<string>) => {
                // 处理结果
                console.log("test-connect", result);
                if (result.code < 0) {
                    reject(ResultDTO.fail(result.message))
                } else {
                    resolve(true)
                }
            })
    })
}
//测试链接完成后打开链接
const openConnectAndReturnDatabases = (connectInfo: MySqlConnectInfo): Promise<ResultDTO<any>> => {
    return new Promise<ResultDTO<any>>((resolve, reject) => {
        ipcRenderer.invoke('open-connect-and-return-databases', JSON.stringify(connectInfo))
            .then(res => {
                    console.log("result,dabases:", res)
                    resolve(ResultDTO.success(res))
                }
            )
            .catch((error) => {
                reject(ResultDTO.fail(error.message))
            })
    })

}

//关闭链接
const closeConnect = (connectId: number, node: TreeOption) => {
    return new Promise<boolean>((resolve, reject) => {
        ipcRenderer.invoke('close-connect', connectId)
            .then(res => {
                resolve(JSON.parse(res))
            }).catch(res => {
            reject(JSON.parse(res))

        })
    })
}

//DataSourceTree.vue使用
const listenToMainEvent = (tree: TreeOption[], map: Map<number, MySqlConnectInfo>) => {
    ipcRenderer.on('main-dispatch-event', (event, args) => {

        let eventName = args[0];
        console.log("接收主线程转发的事件内容", args)
        if (eventName === DispatchEventEnums.FRESH_TREE_NODE_EVENT.code) {
            let connectInfo: MySqlConnectInfo = JSON.parse(args[1]);

            if (map.has(<number>connectInfo.id)) {
                let connectInfoKey: string = '1#' + connectInfo.id;
                for (let i = 0; i < tree.length; i++) {
                    let treeElement: TreeOption = tree[i]
                    //OLD 旧的
                    if (treeElement.key === connectInfoKey) {
                        tree[i] = {
                            label: connectInfo.connectName,
                            key: '1#' + connectInfo.id,
                            isLeaf: false
                        }
                        if (connectInfo.id != null) {
                            map.set(connectInfo.id, connectInfo);
                        }
                    }
                }
            } else {
                tree.push({
                    label: connectInfo.connectName,
                    key: '1#' + connectInfo.id,
                    isLeaf: false
                })
                if (connectInfo.id != null) {
                    map.set(connectInfo.id, connectInfo);
                }
            }
        }
    })
}


export {
    closeConnect, testConnect, openConnectAndReturnDatabases, listenToMainEvent
}